//
//  LoginViewController.m
//  JinChengYun
//
//  Created by 金橙云 on 2017/2/24.
//  Copyright © 2017年 金橙云. All rights reserved.
//

#import "LoginViewController.h"

#import "TodoHttpManager.h"
#import "ViewController.h"
#import "MeViewController.h"
#import "JingViewController.h"
#import "CEOViewController.h"
@interface LoginViewController ()
{
    NSMutableDictionary *_bigDic;
    NSMutableDictionary *_littleDic;
    NSString *tagSt;
}
@property (weak, nonatomic) IBOutlet UITextField *userName;
@property (weak, nonatomic) IBOutlet UITextField *password;
@property (weak, nonatomic) IBOutlet UIButton *backBtn;

@end

@implementation LoginViewController

-(void)viewWillAppear:(BOOL)animated
{
    //单一登录接口通知
      [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didIsTDOnlyLogin:) name:TDOnlyLogin object:nil];
}


-(void)viewWillDisappear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:TDOnlyLogin object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:TDGetLogin object:nil];

}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    _bigDic = [NSMutableDictionary dictionaryWithCapacity:0];
    _littleDic = [NSMutableDictionary dictionaryWithCapacity:0];
    _manager = [TodoHttpManager sharedManager];
    //注册通知登录请求
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didIsTDGetLogin:) name:TDGetLogin object:nil];
    
    
//    _userName.text = [NSString stringWithFormat:@"%@",_userStr];
//    _password.text = [NSString stringWithFormat:@"%@",_passwStr];
}
  // TODO:单一登录接口通知
-(void)didIsTDOnlyLogin:(NSNotification *)tification
{
    if (tification.object != 0)
    {
        int statue = [[tification.object objectForKey:@"status"] intValue];
        if (statue == 1)
        {
            NSString *LoginStr = [tification.object objectForKey:@"data"];
           
            NSUserDefaults *userDefa = [NSUserDefaults standardUserDefaults];

            [userDefa setObject:LoginStr forKey:@"ONLY_URL"];
         
            [userDefa synchronize];
        
            tagSt = [[NSUserDefaults standardUserDefaults] objectForKey:@"STATE"];
             NSLog(@"-------%@",tagSt);
            NSString *Id = [[NSUserDefaults standardUserDefaults] objectForKey:@"BTG"];
            NSLog(@"-------%@",Id);
            // "3" CEO
            if ([tagSt isEqualToString:Id])
            {
               // NSLog(@"1111----%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"STATE"]);
                [_manager userGetLoginWithUserName:_userName.text andPwd:_password.text andUid:Id];
//                [_manager MakeMyselfAlert:@"登陆成功" andIsNeedTitle:NO andShowTime:1.0];
            }
//            else if ([tagSt isEqualToString:@"2"]&&[Id isEqualToString:@"2"])
//            {
//                // "2" 经理
//              //  NSLog(@"2222----%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"STATE"]);
//                [_manager userGetLoginWithUserName:_userName.text andPwd:_password.text andUid:@"2"];
//            }
//            else if ([tagSt isEqualToString:@"1"]&&[Id isEqualToString:Id])
//            {
//                //"1" 置业顾问
//                //NSLog(@"3333----%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"STATE"]);
//                [_manager userGetLoginWithUserName:_userName.text andPwd:_password.text andUid:Id];
//               
//            }
            
        }else
        {
            [_manager MakeMyselfAlert:@"账号和身份不符" andIsNeedTitle:NO andShowTime:2.0];
        }

    }
}

-(void)didIsTDGetLogin:(NSNotification *)notifica
{
  
   
    _bigDic = [notifica.object objectForKey:@"data"];
    _littleDic = [_bigDic objectForKey:@"uid"];
    //NSLog(@"--------%@",notifica.object);

    //登录成功
    int status = [[notifica.object objectForKey:@"status"] intValue];
    if (status == 100 )
    {
        [_manager MakeMyselfAlert:@"登陆成功" andIsNeedTitle:NO andShowTime:1.0];
        NSUserDefaults *userDefa = [NSUserDefaults standardUserDefaults];
        [userDefa setObject:_littleDic forKey:@"UID"];

        [userDefa setObject:[_bigDic objectForKey:@"name"] forKey:@"LOGIN_NAME"];
        [userDefa setObject:[_bigDic objectForKey:@"project_name"] forKey:@"PROJECT_NAME"];
        [userDefa setObject:[[notifica.object objectForKey:@"data"] objectForKey:@"user_id"] forKey:@"USER_ID"];
        [userDefa synchronize];
    
        //置业顾问
        MeViewController *meVC = [[MeViewController alloc] init];
        [self presentViewController:meVC animated:NO completion:nil];
        // NSLog(@"111----存的是---%@",[userDefa setObject:_littleDic forKey:@"UID"]);
    }
    else if (status == 200 )
    {
        [_manager MakeMyselfAlert:@"登陆成功" andIsNeedTitle:NO andShowTime:1.0];
        //销售经理
        NSUserDefaults *userDefa = [NSUserDefaults standardUserDefaults];
        [userDefa setObject:_littleDic forKey:@"UID"];
        [userDefa setObject:[_bigDic objectForKey:@"name"] forKey:@"LOGIN_NAME"];
        [userDefa setObject:[_bigDic objectForKey:@"project_name"] forKey:@"PROJECT_NAME"];
        [userDefa setObject:[[notifica.object objectForKey:@"data"] objectForKey:@"user_id"] forKey:@"USER_ID"];
        [userDefa synchronize];
      //NSLog(@"111----存的是---%@",[[userDefa setObject:@"user_id" forKey:@"USER_ID"]);
        
        JingViewController *jingVc = [[JingViewController alloc] init];
        [self presentViewController:jingVc animated:NO completion:nil];
          NSLog(@"----经理----存的是什么---%@",userDefa);
    }
    else if (status == 300)
    {
        //ceo
        [_manager MakeMyselfAlert:@"登陆成功" andIsNeedTitle:NO andShowTime:1.0];
        NSUserDefaults *userDefa = [NSUserDefaults standardUserDefaults];
        [userDefa setObject:_littleDic forKey:@"UID"];
        [userDefa setObject:[_bigDic objectForKey:@"name"] forKey:@"LOGIN_NAME"];
       [userDefa setObject:[_bigDic objectForKey:@"project_name"] forKey:@"PROJECT_NAME"];
        [userDefa setObject:[[notifica.object objectForKey:@"data"] objectForKey:@"user_id"] forKey:@"USER_ID"];
        [userDefa synchronize];
         NSLog(@"----CEO----存的是什么---%@",userDefa);
        
        CEOViewController *ceoVC =[[CEOViewController alloc] init];
        [self presentViewController:ceoVC animated:NO completion:nil];
    }
    else
    {
        [_manager MakeMyselfAlert:@"无权限登录" andIsNeedTitle:NO andShowTime:2.0];
    }
    
    
}
- (IBAction)backBtnClick:(id)sender
{
    ViewController *viewC = [[ViewController alloc] init];
    [self presentViewController:viewC animated:NO completion:nil];
}

- (IBAction)loginBtnCilck:(id)sender
{
    if ([_userName.text isEqualToString:@""]) {
        [_manager MakeMyselfAlert:@"请输入用户名" andIsNeedTitle:NO andShowTime:1.5];
        return;
    }
    if ([_password.text isEqualToString:@""]) {
        [_manager MakeMyselfAlert:@"请输入密码" andIsNeedTitle:NO andShowTime:1.5];
        return;
    }
    
     [_manager userOnlyLoginWithName:_userName.text];
//    NSString *uid = [[NSUserDefaults standardUserDefaults] objectForKey:@"STATE"];
//   // "3" CEO
//    if ([uid isEqualToString:@"3"])
//    {
//       NSLog(@"1111----%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"STATE"]);
//        [_manager userGetLoginWithUserName:_userName.text andPwd:_password.text andUid:@"3"];
//    }
//    else if ([uid isEqualToString:@"2"])
//    {
//        // "2" 经理
//         NSLog(@"2222----%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"STATE"]);
//        [_manager userGetLoginWithUserName:_userName.text andPwd:_password.text andUid:@"2"];
//    }
//    else
//    {
//        //"1" 置业顾问
//         NSLog(@"3333----%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"STATE"]);
//       //
//        [_manager userOnlyLoginWithName:_userName.text];
//    }


}



#pragma mark-关闭键盘

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self.userName resignFirstResponder];
    [self.password resignFirstResponder];
    return YES;
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.userName resignFirstResponder];
    [self.password resignFirstResponder];
}








- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
